#include "common.h"

/**
 *Main Menu
 */
void show_menu(void)
{
	/* Clear screen and blink FUNCTION*/
	printf(CLEAR);
	printf(BLINK);
	printf("    *Welcome to this game!""\n");
	printf(NONE);
    
	/* Show main menu */
	printf(WHITE);
    printf("--------------------------------\n");
    printf("         1.Introduction\n");
    printf("         2.Help\n");
    printf("         3.Game Start\n");
    printf("         4.To Quit\n");
    printf("--------------------------------\n");
    printf("  Please select the appropriate number: 1~4:\n");
    printf(NONE);
}

/**
 *Select Menu
 */
void choice_num(void)
{
	 int choice;
     while(1){ 
		choice = show_choice();
     	switch(choice){
                case 1:
                    printf(CLEAR);
                    show_introduction();
					break;  
                case 2:
                    printf(CLEAR);
                    show_help();
					break;  
				case 3:                    
                    printf(CLEAR);
                    show_gamestart();
					break;  
                case 4:                    
                    printf(CLEAR);
                    show_exit();
					break;
				default:
					printf(RED"ERROR!\n"NONE);
					exit(0);
					break;
           }
      }
}

/**
 *Get input number
 */
int show_choice(void)
{
	int choice;
    while(1){
         while(scanf("%d",&choice) != 1 || choice < 0 || choice > 4){
         	   printf(RED"ERROR! PLEASE RE-ENTER: \n\n"NONE);
		       while(getchar() != '\n');
		 }
        
		 if (choice >= 1 && choice <= 4)
            break;
    }
    return choice;
}

/**
 *1.Introduction
 */
void show_introduction(void)
{
	show_menu();
    
	printf(GREEN);
    printf("\n猜数游戏是一款人机互动的小游戏，\n"); 
    printf("在电脑提示的范围下进行猜数并用键盘输入电脑中。\n"); 
    printf("电脑判断你所输入的数值与电脑默认给出的数值的大小并给出提示，\n"); 
    printf("如果你所输入的数字不是电脑默认的数值则需要重新输入，直到正确为止！\n");
	printf("\n---------------请继续选择1~4:\n");
    printf(NONE);
}

/**
 *2.Help
 */
void show_help(void)//2
{
	show_menu();
    
	printf(GREEN);
    printf("\n1.游戏规则：你只能输入1~100之间的数字,调皮会报错哦!"); 
    printf("\n2.游戏建议：玩家可用对半猜的方法进行游戏，逐渐缩小猜数范围，直到最后猜出正确答案！\n" );
	printf("\n---------------请继续选择1~4:\n");
    printf(NONE);
}

/**
 *3.Start Game
 */
void show_gamestart(void)//3
{
    printf(RED BLINK);
    printf("\n\n游戏开始, 祝你好运!!!\n");
    printf(NONE);
   
    while(1){
    start(right_num);
    }   
}

/**
 *4.Quit Game
 */
void show_exit(void)//4
{
	char x;
  	show_menu();
	
	while(1){
        setbuf(stdin,NULL);
        printf( RED"\nWhether to exit?------y/n:  \n "NONE);
        scanf("%c",&x);
        
		if(x=='y'||x=='Y')
            exit(0);
        else if(x=='n'||x=='N')
			show_menu();
            break;
    }
}
